#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QDebug>//打印位置
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    InitUI();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::InitUI(){
    //定义棋盘的大小
    this->resize((colume+1)*width,(row+1)*height);
    this->setMaximumSize((colume+1)*width,(row+1)*height);
    this->setMinimumSize((colume+1)*width,(row+1)*height);//限制界面的最大范围
    this->setWindowTitle("五子棋");
    startblack=true;
}
void MainWindow::paintEvent(QPaintEvent *event){
    DrawChessBoard();
    DrawMouseChess();
    DrawChessofBoard();
    update();//强制更新界面
}

void MainWindow::DrawChessBoard(){//画棋盘
    QPainter painter(this);//绘图工具
    painter.setPen(QPen(QColor(Qt::black),2));//边框线的颜色和粗细
    painter.setBrush(Qt::darkYellow);//格子的颜色
    //遍历棋盘所有的格子
    for(int i=0;i<colume;i++){
        for(int j=0;j<row;j++){
            painter.drawRect((i+0.5)*width,(j+0.5)*height,width,height);//绘制棋盘的格子（矩形）
            }
        }
}
void MainWindow::DrawMouseChess(){//画跟随鼠标移动的棋子
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));//让棋子的边缘透明
    if(startblack){
        painter.setBrush(Qt::black);
    }
    else{
        painter.setBrush(Qt::white);
    }
    //                                 获取鼠标位置      圆的半径
    painter.drawEllipse(mapFromGlobal(QCursor::pos()),width/2,height/2);//绘制棋子的圆

}
void MainWindow::DrawChessofBoard(){//画棋盘上的棋子
    QPainter painter(this);
    painter.setPen(QPen(QColor(Qt::transparent)));//让棋子的边缘透明
    for(int i=0;i<num_chess.size();i++){
        chess item=num_chess[i];//当前的棋子的样子和位置赋值给棋子的这个类
        if(item.chess_color){
            painter.setBrush(Qt::black);
        }
        else{
            painter.setBrush(Qt::white);
        }
       DrawChessPoint(painter,item.chess_pt);
    }
}
void MainWindow::DrawChessPoint(QPainter &painter, QPoint &point){
    //获取棋子的摆放位置
    QPoint ptCenter((point.x()+0.5)*width,(point.y()+0.5)*height);
    painter.drawEllipse(ptCenter,width/2,height/2);
}
void MainWindow::mousePressEvent(QMouseEvent *event){
    //获取鼠标点击的位置
    qDebug()<<event->pos();
    QPoint pt;//定义点位
    pt.setX((event->pos().x())/width);
    pt.setY((event->pos().y())/height);
    //判断棋子是否存在
    for(int i=0;i<num_chess.size();i++){
        chess item=num_chess[i];
        if(item.chess_pt==pt){
            return;//如果存在就不做任何显示
        }

    }
    //如果不存在就进行绘制并且判断五子是否相连
    chess item(pt,startblack);
    num_chess.append(item);

    //统计4个点位是否连接
    int nLeft      =CountNearItem(item,QPoint(-1,0));
    int nLeftup    =CountNearItem(item,QPoint(-1,1));
    int nLeftdown  =CountNearItem(item,QPoint(-1,-1));
    int nUp        =CountNearItem(item,QPoint(0,1));
    int nDown      =CountNearItem(item,QPoint(0,-1));
    int nRightup   =CountNearItem(item,QPoint(1,1));
    int nRight     =CountNearItem(item,QPoint(1,0));
    int nRightdown =CountNearItem(item,QPoint(1,-1));

    if((nLeft+nRight)>=4||
            (nLeftup+nRightdown)>=4||
            (nLeftdown+nRightup)>=4||
            (nUp+nDown)>=4)
    {
        QString str=startblack?"Black victory":"White victory";
        QMessageBox::information(NULL,"GAME OVER",str,QMessageBox::Yes);
        num_chess.clear();
        return;
    }

    //一方下完棋，换另一方下棋
    startblack=!startblack;

}
int MainWindow::CountNearItem(chess item,QPoint pt){
    int ncount=0;
    item.chess_pt+=pt;
    while(num_chess.contains(item)){
        ncount++;
        item.chess_pt+=pt;
    }
    return ncount;
}














